//
//  SAAllocateDetailVC.m
//  saasRetail
//
//  Created by 黄志刚 on 2022/4/20.
//

#import "SAAllocateDetailVC.h"
#import "SAAllocateDetailCell.h"
#import "SASaleDocumentsHeaderView.h"
#import "YBImageBrowser.h"
#import "SAStockDispatchDetailAPI.h"
#import "SAAllocateDetailModel.h"
#import "SAStockDispatchDispatchProcessAPI.h"
#import "GFAlertView.h"
#import "SAPirntShopAllocateIn.h"
#import "SAPirntShopDistribution.h"

@interface SAAllocateDetailVC ()<UITableViewDelegate, UITableViewDataSource, YBImageBrowserDataSource,HTTPAPIDelegate>

@property (nonatomic, strong) NSString *orderId;
@property (nonatomic, strong) NSString *type;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *grayView2;//标题头
@property (nonatomic, strong) NSArray *imageList;
@property (nonatomic, strong) SAStockDispatchDetailAPI *stockDispatchDetailAPI;
@property (nonatomic, strong) SAStockDispatchDispatchProcessAPI *stockDispatchDispatchProcessAPI;
@property (nonatomic, strong) SAAllocateDetailModel *detailModel;

@property (nonatomic, strong) UILabel *applyEmployees;
@property (nonatomic, strong) UILabel *applyShop;
@property (nonatomic, strong) UILabel *applyDate;

@property (nonatomic, strong) UILabel *bringEmployees;
@property (nonatomic, strong) UILabel *bringShop;
@property (nonatomic, strong) UILabel *bringDate;

@property (nonatomic, strong) UIView *footView;
@property (nonatomic, strong) UIView *footView2;

@property (nonatomic, strong) UIButton *invalidBtn;
@property (nonatomic, strong) UIButton *printBtn;

@property (nonatomic, strong) UIButton *refusedBtn;
@property (nonatomic, strong) UIButton *BringBtn;
@property (nonatomic, strong) UIButton *printBtn2;
@property (nonatomic, strong) SAOverallSetting *overallSetting;

@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) int count;

@property (nonatomic, strong) UILabel *bringTitle;
@property (nonatomic, strong) UILabel *bringTitle2;
@property (nonatomic, strong) UIImageView *imageV;

@end

@implementation SAAllocateDetailVC

-(instancetype)initWithOrderId:(NSString *)orderId AndType:(NSString *)type {
    if (self = [super init]) {
        self.orderId = orderId;
        self.type = type;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

#pragma mark - HTTPAPIDelegate
- (void)fetchData:(id)data fromServerSuccessedWithAPI:(HTTPAPI *)api {
    if (self.stockDispatchDetailAPI == api) {
        self.detailModel = [SAAllocateDetailModel mj_objectWithKeyValues:data[@"data"]];
        self.applyEmployees.text = self.detailModel.storeName;
        self.applyShop.text = self.detailModel.empName;
        self.applyDate.text = self.detailModel.createTime;
        self.bringEmployees.text = self.detailModel.targetStoreName;
        self.bringShop.text = self.detailModel.targetEmpName;
        self.bringDate.text = self.detailModel.receiveTime;
        [self.tableView reloadData];

        if ([self.detailModel.merchantId integerValue] == [SAValueManger shareInStance].userInfo.merchantId) {//申请店
            self.footView.hidden = NO;
            if (![self.detailModel.tradeStatus isEqualToString:@"0"]) {
                self.invalidBtn.hidden = YES;
                self.printBtn.frame = CGRectMake(0, 0, kScreenWidth, kScaleNum(45));
            }
        }else if (self.detailModel.targetMerchantId.length > 0 && [self.detailModel.targetMerchantId integerValue] == [SAValueManger shareInStance].userInfo.merchantId) {//调出店
            self.footView2.hidden = NO;
            if (![self.detailModel.tradeStatus isEqualToString:@"0"]) {
                self.refusedBtn.hidden = YES;
                self.BringBtn.hidden = YES;
                self.printBtn2.frame = CGRectMake(0, 0, kScreenWidth, kScaleNum(45));
            }
        }else {
            self.footView.hidden = YES;
            self.footView2.hidden = YES;
        }
        
        if ([self.detailModel.tradeStatus isEqualToString:@"3"]) { //作废不需要显示
            self.imageV.hidden = NO;
            self.footView.hidden = YES;
            self.footView2.hidden = YES;
        }else if ([self.detailModel.tradeStatus isEqualToString:@"2"]) {
            self.bringTitle.text = @"拒绝人";
            self.bringTitle2.text = @"拒绝时间";
        }
    
    }else if (self.stockDispatchDispatchProcessAPI == api) {
        if ([self.stockDispatchDispatchProcessAPI.tradeStatus isEqualToString:@"1"]) {
            if ([self.type isEqualToString:@"2"]) {
                [Prompt popPromptViewWithMsg:@"已调出" duration:2];
            }else {
                [Prompt popPromptViewWithMsg:@"已接收" duration:2];
            }
        }else if ([self.stockDispatchDispatchProcessAPI.tradeStatus isEqualToString:@"2"]) {
            [Prompt popPromptViewWithMsg:@"已拒绝" duration:2];
        }else {
            [Prompt popPromptViewWithMsg:@"已作废" duration:2];
        }
        !self.refreshBlock ?: self.refreshBlock();
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)fetchDataFromServerFailedWithError:(NSError *)error API:(HTTPAPI *)api {
    NSDictionary *dic = error.userInfo;
    NSString *msg = [NSString stringWithFormat:@"%@",dic[@"msg"]];
    [Prompt popPromptViewWithMsg:msg duration:2];
}

#pragma mark - UITableView Delegate & DataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.detailModel.detailVOS.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    dispatchDetailModel *model = self.detailModel.detailVOS[section];
    return model.dispatchSKUDetailVOS.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SAAllocateDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SAAllocateDetailCell" forIndexPath:indexPath];
    dispatchDetailModel *detailModel = self.detailModel.detailVOS[indexPath.section];
    dispatchSKUDetailModel *model = detailModel.dispatchSKUDetailVOS[indexPath.row];
    cell.model = model;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kScaleNum(40);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    SASaleDocumentsHeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"SASaleDocumentsHeaderView"];
    dispatchDetailModel *model = self.detailModel.detailVOS[section];
    header.detailModel = model;
    WeakSelf
    header.imageBlock = ^{
        [weakSelf showImage:model.imgList];
    };
    return header;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] initWithFrame:CGRectZero];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kScaleNum(40);
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}

#pragma mark -event
-(void)clickedInvalidBtn {
    GFAlertView *alertView = [GFAlertView alertViewWithMsg:@"提示" detailMsg:@"确认作废该单据吗?" knowBtnBlock:^(UIButton *knowBtn) {
        [knowBtn addTarget:self action:@selector(clickedInvalidBtn2) forControlEvents:UIControlEventTouchUpInside];
    }];
    [alertView showWithAnimation];
}

-(void)clickedInvalidBtn2 {
    self.stockDispatchDispatchProcessAPI.dispatchOrderNo = self.orderId;
    self.stockDispatchDispatchProcessAPI.tradeStatus = @"3";
    [self.stockDispatchDispatchProcessAPI sendRequestToServerWithType:POST];
}

-(void)clickedRefusedBtn {
    GFAlertView *alertView = [GFAlertView alertViewWithMsg:@"提示" detailMsg:@"确认拒绝该单据吗?" knowBtnBlock:^(UIButton *knowBtn) {
        [knowBtn addTarget:self action:@selector(clickedRefusedBtn2) forControlEvents:UIControlEventTouchUpInside];
    }];
    [alertView showWithAnimation];
}

-(void)clickedRefusedBtn2 {
    self.stockDispatchDispatchProcessAPI.dispatchOrderNo = self.orderId;
    self.stockDispatchDispatchProcessAPI.tradeStatus = @"2";
    [self.stockDispatchDispatchProcessAPI sendRequestToServerWithType:POST];
}

-(void)clickedBringBtn {
    NSString *tip = @"确认调出该单据吗?";
    if ([self.type isEqualToString:@"1"]) {
        tip = @"确认接收该单据吗?";
    }
    GFAlertView *alertView = [GFAlertView alertViewWithMsg:@"提示" detailMsg:tip knowBtnBlock:^(UIButton *knowBtn) {
        [knowBtn addTarget:self action:@selector(clickedBringBtn2) forControlEvents:UIControlEventTouchUpInside];
    }];
    [alertView showWithAnimation];
}

-(void)clickedBringBtn2 {
    self.stockDispatchDispatchProcessAPI.dispatchOrderNo = self.orderId;
    self.stockDispatchDispatchProcessAPI.tradeStatus = @"1";
    [self.stockDispatchDispatchProcessAPI sendRequestToServerWithType:POST];
}

-(void)clickedPrintBtn {
    [self startTime];
    if (self.overallSetting.printerSetting.printerStatus == 2 && [SAValueManger shareInStance].printType == 1) {
        if ([self.type isEqualToString:@"2"]) {
            [SAPirntShopAllocateIn printWithTotalData:self.detailModel printSuccessBlock:^{
                
            }];
        }else {
            [SAPirntShopDistribution printWithTotalData:self.detailModel printSuccessBlock:^{
                
            }];
        }
    }else {
        [Prompt popPromptViewWithMsg:@"未连接打印机" duration:2];
    }
}

-(void)startTime {
    self.printBtn.userInteractionEnabled = NO;
    [self.printBtn setTitle:@"打印" forState:UIControlStateNormal];
    self.count = 3;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired) userInfo:nil repeats:YES];
}

- (void)timerFired {
    if (self.count != 1) {
        self.count -= 1;
    } else {
        self.printBtn.userInteractionEnabled = YES;
        [self.printBtn setTitle:@"打印" forState:UIControlStateNormal];
        // 停掉定时器
        [self.timer invalidate];
        self.timer = nil;
    }
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat y = scrollView.contentOffset.y;
    self.grayView2.hidden = y < kScaleNum(180);
}

-(void)showImage:(NSArray *)imageList {
    self.imageList = imageList;
    if (self.imageList.count > 0) {
        NSMutableArray *imageMarr = [NSMutableArray array];
        for (NSString *url in self.imageList) {
            NSString *newUrl = [url stringByReplacingOccurrencesOfString:@"?x-oss-process=style/thumbnail" withString:@""];
            NSString *imageUrl = [NSString stringWithFormat:@"%@?x-oss-process=image/resize,l_720",newUrl];
            [imageMarr addObject:imageUrl];
        }
        self.imageList = imageMarr;
        YBImageBrowser *browser = [YBImageBrowser new];
        browser.dataSourceArray = self.imageList;
        browser.dataSource = self;
        browser.currentPage = 0;
        [browser show];
    }else {
        [Prompt popPromptViewWithMsg:@"该商品没有图片" duration:2];
    }
}

//YBImageBrowserDataSource 代理实现赋值数据
- (NSInteger)yb_numberOfCellsInImageBrowser:(YBImageBrowser *)imageBrowser {
    return self.imageList.count;
}

- (id<YBIBDataProtocol>)yb_imageBrowser:(YBImageBrowser *)imageBrowser dataForCellAtIndex:(NSInteger)index {
    NSString *urlStr = [self.imageList objectAtIndex:index];
    YBIBImageData *data = [YBIBImageData new];
    data.imageURL = [NSURL URLWithString:urlStr];
    return data;
}

#pragma mark - initUI
-(void)initUI {
    [self setNavigationItemBackBBIAndTitle:self.orderId];
    [self setBgImage:nil];
    [self.view addSubview:self.tableView];
    self.overallSetting = [SAOverallSetting shareInstance];
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScaleNum(321))];
    headerView.backgroundColor = kBgColor;
    self.tableView.tableHeaderView = headerView;
    
    UIView *buleView = [[UIView alloc] initWithFrame:SARectMake(20, 10, 4, 20)];
    buleView.backgroundColor = kBuleColor;
    [headerView addSubview:buleView];
    
    UILabel *titleLab = [[UILabel alloc] initWithFrame:SARectMake(41, 0, 200, 40)];
    titleLab.text = @"申请店信息";
    titleLab.textColor = kBlackColor;
    titleLab.font = kFont(16);
    [headerView addSubview:titleLab];
    
    UIView *whiteView = [[UIView alloc] initWithFrame:SARectMake(0, 40, kBaseScreenWidth, 103)];
    whiteView.backgroundColor = [UIColor whiteColor];
    [headerView addSubview:whiteView];
    
    UIButton *applyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    applyBtn.frame = SARectMake(20, 0, 100, 40);
    [applyBtn setTitle:@"    申请店" forState:UIControlStateNormal];
    [applyBtn setImage:[UIImage imageNamed:@"Allocate_top"] forState:UIControlStateNormal];
    applyBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [applyBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
    applyBtn.titleLabel.font = kBFont(16);
    [whiteView addSubview:applyBtn];
    
    UILabel *applyEmployees = [[UILabel alloc] initWithFrame:SARectMake(kBaseScreenWidth - 220, 0, 200, 40)];
    applyEmployees.textColor = kBlackColor;
    applyEmployees.font = kBFont(16);
    applyEmployees.textAlignment = NSTextAlignmentRight;
    [whiteView addSubview:applyEmployees];
    self.applyEmployees = applyEmployees;
    
    UIView *line = [[UIView alloc] initWithFrame:SARectMake(20, 40, kBaseScreenWidth - 40, 1)];
    line.backgroundColor = kLineColor;
    [whiteView addSubview:line];
    
    UILabel *applyTitle = [[UILabel alloc] initWithFrame:SARectMake(20, 44, 100, 26)];
    applyTitle.text = @"申请人";
    applyTitle.textColor = kLightgGrayColor;
    applyTitle.font = kFont(16);
    applyTitle.textAlignment = NSTextAlignmentLeft;
    [whiteView addSubview:applyTitle];
    
    UILabel *applyTitle2 = [[UILabel alloc] initWithFrame:SARectMake(20, 70, 100, 26)];
    applyTitle2.text = @"申请时间";
    applyTitle2.textColor = kLightgGrayColor;
    applyTitle2.font = kFont(16);
    applyTitle2.textAlignment = NSTextAlignmentLeft;
    [whiteView addSubview:applyTitle2];
    
    UILabel *applyShop = [[UILabel alloc] initWithFrame:SARectMake(kBaseScreenWidth - 220, 44, 200, 26)];
    applyShop.textColor = kLightgGrayColor;
    applyShop.font = kFont(16);
    applyShop.textAlignment = NSTextAlignmentRight;
    [whiteView addSubview:applyShop];
    self.applyShop = applyShop;
    
    UILabel *applyDate = [[UILabel alloc] initWithFrame:SARectMake(kBaseScreenWidth - 220, 70, 200, 26)];
    applyDate.textColor = kLightgGrayColor;
    applyDate.font = kFont(16);
    applyDate.textAlignment = NSTextAlignmentRight;
    [whiteView addSubview:applyDate];
    self.applyDate = applyDate;
    
    UIView *buleView2 = [[UIView alloc] initWithFrame:SARectMake(20, 153, 4, 20)];
    buleView2.backgroundColor = kBuleColor;
    [headerView addSubview:buleView2];
    
    UILabel *titleLab2 = [[UILabel alloc] initWithFrame:SARectMake(41, 143, 200, 40)];
    titleLab2.text = @"调出店信息";
    titleLab2.textColor = kBlackColor;
    titleLab2.font = kFont(16);
    [headerView addSubview:titleLab2];
    
    UIView *whiteView2 = [[UIView alloc] initWithFrame:SARectMake(0, 183, kBaseScreenWidth, 103)];
    whiteView2.backgroundColor = [UIColor whiteColor];
    [headerView addSubview:whiteView2];
    
    UIButton *bringBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    bringBtn.frame = SARectMake(20, 0, 100, 40);
    [bringBtn setTitle:@"    调出店" forState:UIControlStateNormal];
    [bringBtn setImage:[UIImage imageNamed:@"Allocate_down"] forState:UIControlStateNormal];
    bringBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [bringBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
    bringBtn.titleLabel.font = kBFont(16);
    [whiteView2 addSubview:bringBtn];
    
    UILabel *bringEmployees = [[UILabel alloc] initWithFrame:SARectMake(kBaseScreenWidth - 220, 0, 200, 40)];
    bringEmployees.textColor = kBlackColor;
    bringEmployees.font = kBFont(16);
    bringEmployees.textAlignment = NSTextAlignmentRight;
    [whiteView2 addSubview:bringEmployees];
    self.bringEmployees = bringEmployees;
    
    UIView *line2 = [[UIView alloc] initWithFrame:SARectMake(20, 40, kBaseScreenWidth - 40, 1)];
    line2.backgroundColor = kLineColor;
    [whiteView2 addSubview:line2];
    
    UILabel *bringTitle = [[UILabel alloc] initWithFrame:SARectMake(20, 44, 100, 26)];
    bringTitle.text = @"调出人";
    bringTitle.textColor = kLightgGrayColor;
    bringTitle.font = kFont(16);
    bringTitle.textAlignment = NSTextAlignmentLeft;
    [whiteView2 addSubview:bringTitle];
    self.bringTitle = bringTitle;
    
    UILabel *bringTitle2 = [[UILabel alloc] initWithFrame:SARectMake(20, 70, 100, 26)];
    bringTitle2.text = @"调出时间";
    bringTitle2.textColor = kLightgGrayColor;
    bringTitle2.font = kFont(16);
    bringTitle2.textAlignment = NSTextAlignmentLeft;
    [whiteView2 addSubview:bringTitle2];
    self.bringTitle2 = bringTitle2;
    
    UILabel *bringShop = [[UILabel alloc] initWithFrame:SARectMake(kBaseScreenWidth - 220, 44, 200, 26)];
    bringShop.textColor = kLightgGrayColor;
    bringShop.font = kFont(16);
    bringShop.textAlignment = NSTextAlignmentRight;
    [whiteView2 addSubview:bringShop];
    self.bringShop = bringShop;
    
    UILabel *bringDate = [[UILabel alloc] initWithFrame:SARectMake(kBaseScreenWidth - 220, 70, 200, 26)];
    bringDate.textColor = kLightgGrayColor;
    bringDate.font = kFont(16);
    bringDate.textAlignment = NSTextAlignmentRight;
    [whiteView2 addSubview:bringDate];
    self.bringDate = bringDate;
    
    UIView *grayView = [[UIView alloc] initWithFrame:CGRectMake(0, kScaleNum(286), kScreenWidth, kScaleNum(35))];
    grayView.backgroundColor = kTextBgColor;
    [headerView addSubview:grayView];
    NSArray *arr2 = @[@"商品", @"调货数量", @"单价", @"小计"];
    if ([self.type isEqualToString:@"1"]) {
        arr2 = @[@"商品", @"配货数量", @"单价", @"小计"];
        titleLab.text = @"配出店信息";
        [applyBtn setTitle:@"    配出店" forState:UIControlStateNormal];
        applyTitle.text = @"配货人";
        applyTitle2.text = @"配货时间";
        titleLab2.text = @"接收店信息";
        [bringBtn setTitle:@"    接收店" forState:UIControlStateNormal];
        bringTitle.text = @"接收人";
        bringTitle2.text = @"接收时间";
    }
    for (int i = 0; i < arr2.count; i++) {
        UILabel *titleLab = [[UILabel alloc] init];
        titleLab.text = arr2[i];
        titleLab.font = kFont(16);
        titleLab.textColor = kTextColor;
        [grayView addSubview:titleLab];
        titleLab.textAlignment = NSTextAlignmentCenter;
        if (i == 0) {
            titleLab.frame = CGRectMake(kScaleNum(20), 0, kScaleNum(60), kScaleNum(35));
            titleLab.textAlignment = NSTextAlignmentLeft;
        }else if (i == 1) {
            titleLab.frame = CGRectMake(kScaleNum(110), 0, kScaleNum(70), kScaleNum(35));
        }else if (i == 2) {
            titleLab.frame = CGRectMake(kScaleNum(210), 0, kScaleNum(70), kScaleNum(35));
        }else if (i == 3) {
            titleLab.frame = CGRectMake(kScaleNum(290), 0, kScaleNum(80), kScaleNum(35));
        }
    }
    
    UIView *grayView2 = [[UIView alloc] initWithFrame:CGRectMake(0, kScaleNum(StatusHeight)-0.5, kScreenWidth, kScaleNum(35))];
    grayView2.backgroundColor = kColor(228, 231, 243, 1);
    grayView2.hidden = YES;
    [self.view addSubview:grayView2];
    for (int i = 0; i < arr2.count; i++) {
        UILabel *titleLab = [[UILabel alloc] init];
        titleLab.text = arr2[i];
        titleLab.font = kFont(15);
        titleLab.textColor = kTextColor;
        [grayView2 addSubview:titleLab];
        titleLab.textAlignment = NSTextAlignmentCenter;
        if (i == 0) {
            titleLab.frame = CGRectMake(kScaleNum(20), 0, kScaleNum(60), kScaleNum(35));
            titleLab.textAlignment = NSTextAlignmentLeft;
        }else if (i == 1) {
            titleLab.frame = CGRectMake(kScaleNum(110), 0, kScaleNum(70), kScaleNum(35));
        }else if (i == 2) {
            titleLab.frame = CGRectMake(kScaleNum(210), 0, kScaleNum(70), kScaleNum(35));
        }else if (i == 3) {
            titleLab.frame = CGRectMake(kScaleNum(290), 0, kScaleNum(80), kScaleNum(35));
        }
    }
    self.grayView2 = grayView2;
    
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - kScaleNum(45) - SafeFooterHeight, kScreenWidth, kScaleNum(45))];
    [self.view addSubview:footView];
    footView.hidden = YES;
    self.footView = footView;
    
    UIButton *invalidBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    invalidBtn.frame = CGRectMake(0, 0, kScreenWidth/2, kScaleNum(45));
    [invalidBtn setTitle:@"作废" forState:UIControlStateNormal];
    invalidBtn.backgroundColor = kColor(205, 205, 205, 1);
    invalidBtn.titleLabel.font = kBFont(18);
    [invalidBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [invalidBtn addTarget:self action:@selector(clickedInvalidBtn) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:invalidBtn];
    self.invalidBtn = invalidBtn;

    UIButton *printBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    printBtn.frame = CGRectMake(kScreenWidth/2, 0, kScreenWidth/2, kScaleNum(45));
    [printBtn setTitle:@"打印" forState:UIControlStateNormal];
    printBtn.titleLabel.font = kBFont(18);
    printBtn.backgroundColor = kPinkColor;
    [printBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [printBtn addTarget:self action:@selector(clickedPrintBtn) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:printBtn];
    self.printBtn = printBtn;
    
    UIView *footView2 = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - kScaleNum(45) - SafeFooterHeight, kScreenWidth, kScaleNum(45))];
    [self.view addSubview:footView2];
    footView2.hidden = YES;
    self.footView2 = footView2;
    
    UIButton *refusedBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    refusedBtn.frame = CGRectMake(0, 0, kScreenWidth/3, kScaleNum(45));
    [refusedBtn setTitle:@"拒绝" forState:UIControlStateNormal];
    refusedBtn.backgroundColor = kColor(205, 205, 205, 1);
    refusedBtn.titleLabel.font = kBFont(18);
    [refusedBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [refusedBtn addTarget:self action:@selector(clickedRefusedBtn) forControlEvents:UIControlEventTouchUpInside];
    [footView2 addSubview:refusedBtn];
    self.refusedBtn = refusedBtn;
    
    NSString *titleName = @"接收";
    if ([self.type isEqualToString:@"2"]) {
        titleName = @"调出";
    }
    UIButton *BringBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    BringBtn.frame = CGRectMake(kScreenWidth/3, 0, kScreenWidth/3, kScaleNum(45));
    [BringBtn setTitle:titleName forState:UIControlStateNormal];
    BringBtn.backgroundColor = kColor(248, 185, 52, 1);
    BringBtn.titleLabel.font = kBFont(18);
    [BringBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [BringBtn addTarget:self action:@selector(clickedBringBtn) forControlEvents:UIControlEventTouchUpInside];
    [footView2 addSubview:BringBtn];
    self.BringBtn = BringBtn;

    UIButton *printBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    printBtn2.frame = CGRectMake(kScreenWidth*2/3, 0, kScreenWidth/3, kScaleNum(45));
    [printBtn2 setTitle:@"打印" forState:UIControlStateNormal];
    printBtn2.titleLabel.font = kBFont(18);
    printBtn2.backgroundColor = kPinkColor;
    [printBtn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [printBtn2 addTarget:self action:@selector(clickedPrintBtn) forControlEvents:UIControlEventTouchUpInside];
    [footView2 addSubview:printBtn2];
    self.printBtn2 = printBtn2;
    
    UIImageView *imageV = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pink_invalid"]];
    imageV.frame = CGRectMake(kScreenWidth - kScaleNum(90), kScaleNum(10), kScaleNum(80), kScaleNum(59));
    [headerView addSubview:imageV];
    imageV.hidden = YES;
    self.imageV = imageV;
    
    self.stockDispatchDetailAPI.dispatchOrderNo = self.orderId;
    [self.stockDispatchDetailAPI sendRequestToServerWithType:GET];
}

#pragma mark - lazy
-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kScaleNum(StatusHeight), kScreenWidth, kScreenHeight - kScaleNum(StatusHeight + 45) - SafeFooterHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = kBgColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsHorizontalScrollIndicator = NO;
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        [_tableView registerClass:[SAAllocateDetailCell class] forCellReuseIdentifier:@"SAAllocateDetailCell"];
        [_tableView registerClass:[SASaleDocumentsHeaderView class] forHeaderFooterViewReuseIdentifier:@"SASaleDocumentsHeaderView"];
    }
    return _tableView;
}

-(SAStockDispatchDetailAPI *)stockDispatchDetailAPI {
    if (!_stockDispatchDetailAPI) {
        _stockDispatchDetailAPI = [SAStockDispatchDetailAPI apiWithDelegate:self];
    }
    return _stockDispatchDetailAPI;
}

-(SAStockDispatchDispatchProcessAPI *)stockDispatchDispatchProcessAPI {
    if (!_stockDispatchDispatchProcessAPI) {
        _stockDispatchDispatchProcessAPI = [SAStockDispatchDispatchProcessAPI apiWithDelegate:self];
    }
    return _stockDispatchDispatchProcessAPI;
}

@end
